<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <son @handler="getData"></son>
    </div>

    <template id="tmp">
      <div>
        <h2>{{msg}}</h2>
        <button @click="send">click</button>
      </div>
    </template>
    <script src="./vue.js"></script>
    <script>
      Vue.component("son", {
        data() {
          return {
            msg: "son's data",
          };
        },
        methods: {
          send() {
            //触发当前实例上的handler事件 并传递this.msg作为参数
            this.$emit("handler", this.msg);
          },
        },
        template: "#tmp",
      });

      var vm = new Vue({
        el: "#app",
        data: {
          getMsg: "",
        },
        methods: {
          getData(res) {
            console.log(res);
            this.getMsg = res;
          },
        },
      });
    </script>
  </body>
</html>
